package com.base.springboot.starter.redisson.service;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.ClusterServersConfig;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

import javax.annotation.Resource;

/**
 * 配置类
 *
 * @Author：admin
 * @Date 2020/6/11 13:47
 */
@Configuration
public class RedissonClusterConfiguration {
    @Resource
    private Environment environment;
    /**
     * 集群节点
     */
    @Value("${spring.redis.cluster.nodes}")
    private String cluster;

    /**
     * 等待节点回复命令的时间。该时间从命令发送成功时开始计时。
     */
    @Value("${spring.redis.timeout:5000}")
    private Integer timeout;
    /**
     * 集群状态扫描间隔时间，单位是毫秒
     */
    @Value("${spring.redis.cluster.scan-interval:1000}")
    private Integer scanInterval;
    /**
     *
     */
    @Value("${spring.redis.cluster.failed-attempts:3}")
    private Integer failedAttempts;
    /**
     * 重试的次数
     */
    @Value("${spring.redis.cluster.retry-attempts:3}")
    private Integer retryAttempts;
    /**
     * 重试的时间间隔，单位是毫秒
     */
    @Value("${spring.redis.cluster.retry-interval:1500}")
    private Integer retryInterval;
    /**
     * slave节点的连接池中连接数最大为500
     */
    @Value("${spring.redis.cluster.slave-connection-pool-size:64}")
    private Integer slaveConnectionPoolSize;
    /**
     * master节点的连接池中连接数最大为500
     */
    @Value("${spring.redis.cluster.master-connection-pool-size:64}")
    private Integer masterConnectionPoolSize;
    /**
     * ping连接间隔  客户端长时间未使用，服务端会断开
     */
    @Value("${spring.redis.cluster.pingConnectionInterval:30000}")
    private Integer pingConnectionInterval;

    @Bean
    public RedissonClient getRedisson() {
        Config config = new Config();

        //这是用的集群server
        ClusterServersConfig clusterServersConfig = config.useClusterServers();

        String[] nodes = cluster.split(",");
        for (int i = 0; i < nodes.length; i++) {
            clusterServersConfig.addNodeAddress("redis://" + nodes[i]);
        }

        if (!ObjectUtil.isNull(scanInterval)) {
            clusterServersConfig.setScanInterval(scanInterval);
        }

        if (!ObjectUtil.isNull(retryAttempts)) {
            clusterServersConfig.setRetryAttempts(retryAttempts);
        }

        if (!ObjectUtil.isNull(retryInterval)) {
            clusterServersConfig.setRetryInterval(retryInterval);
        }

        if (!ObjectUtil.isNull(masterConnectionPoolSize)) {
            clusterServersConfig.setMasterConnectionMinimumIdleSize(masterConnectionPoolSize);
        }

        if (!ObjectUtil.isNull(slaveConnectionPoolSize)) {
            clusterServersConfig.setSlaveConnectionMinimumIdleSize(slaveConnectionPoolSize);
        }

        if (!ObjectUtil.isNull(timeout)) {
            clusterServersConfig.setTimeout(timeout);
        }
        if(!ObjectUtil.isNull(pingConnectionInterval)){
            clusterServersConfig.setPingConnectionInterval(pingConnectionInterval);
        }

        String password = environment.getProperty("spring.redis.password");
        if (!StrUtil.isBlank(password)) {
            clusterServersConfig.setPassword(password);
        }

        return Redisson.create(config);
    }
}
